package com.metal.gear.certification;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Line2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.*;

/***
 * 岩屑剖面
 *
 */
class CuttingsBisect {

    public static void main(String[] args) throws Exception {
        CuttingsBisect cuttingsBisect = new CuttingsBisect();
        cuttingsBisect.contextLoads();
    }

    void contextLoads() throws Exception {
        int width = 1000, height = 3000;
        //创建图片对象
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);
        //基于图片对象打开绘图
        Graphics2D graphics = image.createGraphics();
        //设置属性
        graphics.setFont(new Font("微软雅黑", Font.PLAIN, 35));
        graphics.setColor(Color.BLACK);

        //绘制标题
        float picStartX = 0;
        float picStartY = 0;
        float titleHeight = 100;
        float titleWeight1 = 100;
        float titleWeight2 = 200;
        float titleWeight3 = 200;
        drawTitle(graphics, picStartX, picStartY, titleHeight, titleWeight1, titleWeight2, titleWeight3);
        float depthGaugeHeight = 200;
        int layerNum = 5;
        drawDepthGauge(graphics, picStartX, picStartY, depthGaugeHeight, titleHeight, titleWeight1, layerNum);


        drawGeologicalLegend(graphics, picStartX, picStartY, depthGaugeHeight, titleHeight, titleWeight1, titleWeight2, layerNum);
        drawCuttingPic(graphics, picStartX, picStartY, depthGaugeHeight, titleHeight, titleWeight1, titleWeight2, titleWeight3, layerNum);
//        float picStartX, float picStartY,
//        float depthGaugeHeight,
//        float titleHeight,
//        float titleWeight1, float titleWeight2,
//        int layerNum) {


        // 绘图逻辑 END
        //处理绘图
        graphics.dispose();
        //将绘制好的图片写入到图片
        ImageIO.write(image, "png", new File("abc.png"));
    }


    /***
     * 绘制标题
     * 标题分为三个部分
     * 1、深度
     * 2、岩性剖面地质符号
     * 3、岩屑影像
     *
     * @param graphics
     * @param picStartX  图片起始位置X坐标
     * @param picStartY  图片起始位置Y坐标
     * @param titleHeight  标题高度
     * @param titleWeight1 标题“深度”宽度
     * @param titleWeight2 标题“岩性剖面”宽度
     * @param titleWeight3 标题“岩屑影像”宽度
     */
    public void drawTitle(Graphics2D graphics,
                          float picStartX, float picStartY,
                          float titleHeight, float titleWeight1, float titleWeight2, float titleWeight3) {
        Rectangle2D.Float rectangle1 = new Rectangle2D.Float(picStartX, picStartY, titleWeight1, titleHeight);
        Rectangle2D.Float rectangle2 = new Rectangle2D.Float(picStartX + titleWeight1, picStartY, titleWeight2, titleHeight);
        Rectangle2D.Float rectangle3 = new Rectangle2D.Float(picStartX + titleWeight1 + titleWeight2, picStartY, titleWeight3, titleHeight);

        graphics.draw(rectangle1);
        graphics.draw(rectangle2);
        graphics.draw(rectangle3);

        //-----------------------------------------------------------------------------------
        graphics.drawString("深度", 20, 60);
        graphics.drawString("岩性图例", 120, 60);
        graphics.drawString("岩屑影像", 320, 60);

    }


    /***
     * 绘制深度尺
     * @param graphics
     * @param picStartX  图片起始位置X坐标
     * @param picStartY  图片起始位置Y坐标
     * @param depthGaugeHeight 深度尺高度
     * @param titleHeight 标题高度
     * @param titleWeight1 标题1宽度
     * @param layerNum 深度尺层数
     */
    public void drawDepthGauge(Graphics2D graphics,
                               float picStartX, float picStartY,
                               float depthGaugeHeight,
                               float titleHeight, float titleWeight1, int layerNum) {
        float depthGaugeWeight = titleWeight1;
        float depthGaugeGraduation = depthGaugeHeight / 10;
        //深度尺刻度线宽度
        float depthGaugeGraduationWeight = 20f;


        for (int i = 0; i < layerNum; i++) {
            float x = picStartX;
            float y = picStartY + titleHeight + i * depthGaugeHeight;
            Rectangle2D.Float rectangle = new Rectangle2D.Float(x, y, depthGaugeWeight, depthGaugeHeight);
            graphics.draw(rectangle);

            for (int j = 1; j <= 9; j++) {
                float x1 = x + depthGaugeWeight - depthGaugeGraduationWeight;
                float y1 = titleHeight + i * depthGaugeHeight + depthGaugeGraduation * j;
                float x2 = x + depthGaugeWeight;
                float y2 = titleHeight + depthGaugeGraduation * j + i * depthGaugeHeight;
                Line2D.Float aFloat = new Line2D.Float(x1, y1, x2, y2);
                graphics.draw(aFloat);
            }
        }
    }

    /***
     *  加载地质图例
     *
     * @param graphics
     * @param picStartX  图片起始位置X坐标
     * @param picStartY  图片起始位置Y坐标
     * @param depthGaugeHeight 深度尺高度
     * @param titleHeight 标题高度
     * @param titleWeight1 标题1宽度
     * @param titleWeight2 标题2宽度
     * @param layerNum 深度尺层数
     */
    private void drawGeologicalLegend(Graphics2D graphics,
                                      float picStartX, float picStartY,
                                      float depthGaugeHeight,
                                      float titleHeight,
                                      float titleWeight1, float titleWeight2,
                                      int layerNum) {
        for (int i = 0; i < layerNum; i++) {
            float dx1 = picStartX + titleWeight1;
            float dy1 = picStartY + titleHeight + i * depthGaugeHeight;
            float dx2 = picStartX + titleWeight1 + titleWeight2;
            float dy2 = picStartX + titleHeight + (i + 1) * depthGaugeHeight;

            BufferedImage cuttingImage = loadImage();
            switch (i){
                case 0:
                    graphics.drawImage(cuttingImage,
                            (int) dx1, (int) dy1, (int) dx2-20, (int) dy2,
                            0, 0, 180, 100,
                            null);
                    break;
                case 1:
                    graphics.drawImage(cuttingImage,
                            (int) dx1, (int) dy1, (int) dx2-40, (int) dy2,
                            0, 0, 160, 100,
                            null);
                    break;
                case 2:
                    graphics.drawImage(cuttingImage,
                            (int) dx1, (int) dy1, (int) dx2-120, (int) dy2,
                            0, 0, 80, 100,
                            null);
                case 3:

                    graphics.drawImage(cuttingImage,
                            (int) dx1, (int) dy1, (int) dx2, (int) dy2,
                            0, 0, 200, 100,
                            null);
                    break;

                case 4:
                    graphics.drawImage(cuttingImage,
                            (int) dx1, (int) dy1, (int) dx2-100, (int) dy2,
                            0, 0, 100, 100,
                            null);
                    break;

            }
        }
    }

    /***
     *  加载地质图例
     *
     * @param graphics
     * @param picStartX  图片起始位置X坐标
     * @param picStartY  图片起始位置Y坐标
     * @param depthGaugeHeight 深度尺高度
     * @param titleHeight 标题高度
     * @param titleWeight1 标题1宽度
     * @param titleWeight2 标题2宽度
     * @param titleWeight3 标题3宽度
     * @param layerNum 深度尺层数
     */
    private void drawCuttingPic(Graphics2D graphics,
                                float picStartX, float picStartY,
                                float depthGaugeHeight,
                                float titleHeight,
                                float titleWeight1, float titleWeight2, float titleWeight3,
                                int layerNum) {
        for (int i = 0; i < layerNum; i++) {
            float dx1 = picStartX + titleWeight1 + titleWeight2;
            float dy1 = picStartY + titleHeight + i * depthGaugeHeight;
            float dx2 = picStartX + titleWeight1 + titleWeight2 + titleWeight3;
            float dy2 = picStartX + titleHeight + (i + 1) * depthGaugeHeight;

            BufferedImage cuttingImage = loadImage2();
            graphics.drawImage(cuttingImage,
                    (int) dx1, (int) dy1, (int) dx2, (int) dy2,
                    0, 0, 100, 100,
                    null);
        }


    }

    /***
     * 加载岩性地质图片，暂时时只用了一张样例
     *
     * @return
     */
    private BufferedImage loadImage() {
        try {
            return ImageIO.read(new File("C:\\Users\\86136\\Desktop\\岩屑地质符号.png"));
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }


    /***
     * 加载岩性地质图片，暂时时只用了一张样例
     *
     * @return
     */
    private BufferedImage loadImage2() {
        try {
            return ImageIO.read(new File("C:\\Users\\86136\\Desktop\\岩屑图像.png"));
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

}
